﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BIZ;
using BIZ.Common;
using System.IO;

namespace JoyLibrary.Controllers
{
        [DomainConfirmationFilter]
    public class CommonController : Controller
    {

        //tinymce图片文件上传异步方法
        [HttpPost]
        public ActionResult UploadFile()
        {
            if (Request.Files.Count < 1 || Request.Files[0].ContentLength == 0)
                return Content("<script type=\"text/javascript\">var result = { error: \"您没有指定上传的文件或文件为空!\" };</script>");

            try
            {
                var file = Request.Files[0];

                var us = new UploadService();
                var url = us.Save(file);
                if (us.Uploaded)
                {
                    return Content(string.Format("<script type=\"text/javascript\">var result = {{ url: \"{0}\" }};</script>", url));
                }
                else
                {
                    return Content(string.Format("<script type=\"text/javascript\">var result = {{ url: \"{0}\", exist:\"true\" }};</script>", url));
                }
            }
            catch (Exception exception)
            {
                return Content(string.Format("<script type=\"text/javascript\">var result = {{ error: \"{0}\" }};</script>", string.Format("{0}", exception.Message)));
            }
        }
        [HttpPost]
        public ActionResult InsertImage()
        {
            return View();
        }

        [NonAction]
        private void saveFile(HttpPostedFileBase postedFile, string filepath, string saveName)
        {
            string phyPath = Request.MapPath("~" + filepath + "/");
            if (!Directory.Exists(phyPath))
            {
                Directory.CreateDirectory(phyPath);
            }
            try
            {
                postedFile.SaveAs(phyPath + saveName);
            }
            catch (Exception e)
            {
                throw new ApplicationException(e.Message);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ContentResult Import(HttpPostedFileBase FileData, string folder)
        {
            string result = "";
            if (null != FileData)
            {
                try
                {     
                    folder = folder + "/ebook/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/";
                    string ext = Path.GetExtension(FileData.FileName);//获得文件扩展名            
                    string saveName = DateTime.Now.ToString("yyyyMMddHHmmss") + ext;//实际保存文件名            
                    saveFile(FileData, folder, saveName);//保存文件      
                    result = folder + saveName;//获得文件名
                }
                catch
                {
                    result = "";
                }
            }
            return Content(result);
        }
    }
}
